package com.YHVision.qiuzhike.controller;

import com.YHVision.qiuzhike.model.dto.company.CompanyQueryRequest;
import com.YHVision.qiuzhike.model.entity.Company;
import com.YHVision.qiuzhike.model.vo.CompanyVO;
import com.YHVision.qiuzhike.service.CompanyService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.YHVision.qiuzhike.annotation.AuthCheck;
import com.YHVision.qiuzhike.common.BaseResponse;
import com.YHVision.qiuzhike.common.DeleteRequest;
import com.YHVision.qiuzhike.common.ErrorCode;
import com.YHVision.qiuzhike.common.ResultUtils;
import com.YHVision.qiuzhike.constant.UserConstant;
import com.YHVision.qiuzhike.exception.BusinessException;
import com.YHVision.qiuzhike.exception.ThrowUtils;
import com.YHVision.qiuzhike.model.dto.companyFavour.CompanyFavourAddRequest;
import com.YHVision.qiuzhike.model.dto.companyFavour.CompanyFavourQueryRequest;
import com.YHVision.qiuzhike.model.entity.CompanyFavour;
import com.YHVision.qiuzhike.model.entity.User;
import com.YHVision.qiuzhike.service.CompanyFavourService;
import com.YHVision.qiuzhike.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 企业收藏接口
 *
 * @author <a href="">YHVision-小y</a>
 * @from <a href="todo">爱编程学习圈</a>
 */
@RestController
@RequestMapping("/companyFavour")
@Slf4j
public class CompanyFavourController {

    @Resource
    private CompanyFavourService companyFavourService;

    @Resource
    private UserService userService;

    @Resource
    private CompanyService companyService;

    /**
     * 收藏/取消收藏（必须登录才能操作）
     *
     * @param companyFavourAddRequest
     * @param request
     * @return
     */
    @PostMapping("/")
    public BaseResponse<Integer> doCompanyFavour(@RequestBody CompanyFavourAddRequest companyFavourAddRequest,
                                                 HttpServletRequest request) {
        //数据校验
        if (companyFavourAddRequest == null || companyFavourAddRequest.getCompanyId() == null || companyFavourAddRequest.getCompanyId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //获取登录用户
        final User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        Long companyId = companyFavourAddRequest.getCompanyId();
        int result = companyFavourService.doCompanyFavour(companyId, loginUser);
        return ResultUtils.success(result);
    }


    /**
     * 分页获取我的收藏列表
     *
     * @param companyQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/my/list/page")
    public BaseResponse<Page<CompanyVO>> listMyFavourCompanyByPage(@RequestBody CompanyQueryRequest companyQueryRequest,
                                                                   HttpServletRequest request) {
        //数据校验
        if (companyQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        final User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        //分页查询
        long current = companyQueryRequest.getCurrent();
        long pageSize = companyQueryRequest.getPageSize();
        //限制爬虫
        ThrowUtils.throwIf(pageSize > 20, ErrorCode.PARAMS_ERROR);
        Page<Company> companyPage = companyFavourService.listCompanyFavourByPage(new Page<>(current, pageSize),
                companyService.getQueryWrapper(companyQueryRequest), loginUser.getId());
        return ResultUtils.success(companyService.getCompanyVOPage(companyPage, request));
    }

    /**
     * 分页获取用户收藏列表
     * @param companyFavourQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<CompanyVO>> listFavourCompanyByPage(@RequestBody CompanyFavourQueryRequest companyFavourQueryRequest
            , HttpServletRequest request) {
        if (companyFavourQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = companyFavourQueryRequest.getCurrent();
        long pageSize = companyFavourQueryRequest.getPageSize();
        Long userId = companyFavourQueryRequest.getUserId();
        //限制爬虫
        ThrowUtils.throwIf(pageSize > 20, ErrorCode.PARAMS_ERROR);
        Page<Company> companyPage = companyFavourService.listCompanyFavourByPage(new Page<>(current, pageSize),
                companyService.getQueryWrapper(companyFavourQueryRequest.getCompanyQueryRequest()), userId);
        return ResultUtils.success(companyService.getCompanyVOPage(companyPage, request));


    }
}

